package edu.zut.main.thread;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

public class ThreadDemo3 {
    public static void main(String[] args) {
        ImgDownThread imgDownThread1 = new ImgDownThread("https://img0.baidu.com/it/u=3998012246,2453684564&fm=26&fmt=auto&gp=0.jpg", "1.jpg");
        ImgDownThread imgDownThread2 = new ImgDownThread("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic27.nipic.com%2F20130307%2F8984340_113532918000_2.jpg&refer=http%3A%2F%2Fpic27.nipic.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1633479648&t=c0c0f5baf9be25692f877ceb166c37d2", "2.jpg");

        new Thread(imgDownThread1).start();
        new Thread(imgDownThread2).start();
    }

}
//模拟同时 从网上下载多张图片
class ImgDownThread implements Runnable{

    private String url;
    private String fileName;

    public ImgDownThread(String url,String fileName){
        this.url = url;
        this.fileName = fileName;
    }

    @Override
    public void run() {
        new ImgDown().down(url,fileName);
        System.out.println("下载的图片是"+fileName);
    }
}
//下载方法
class ImgDown{
    //下载  利用commons-io提供的下载
    public void down(String url,String fileName){
        try {
            FileUtils.copyURLToFile(new URL(url),new File(fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}



